package com.maibow.controller.organ;

import java.io.File;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.maibow.constants.BusinessConstants;
import com.maibow.controller.base.BaseController;
import com.maibow.entity.Page;
import com.maibow.entity.admin.Dictionary;
import com.maibow.entity.admin.SysOperator;
import com.maibow.entity.site.Area;
import com.maibow.entity.site.City;
import com.maibow.entity.site.Province;
import com.maibow.interceptor.Token;
import com.maibow.service.admin.DictionaryService;
import com.maibow.service.admin.NewUserService;
import com.maibow.service.admin.SiteService;
import com.maibow.service.organ.OrganService;
import com.maibow.service.system.CommonService;
import com.maibow.util.CommonConstants;
import com.maibow.util.DateUtil;
import com.maibow.util.FileUpload;
import com.maibow.util.PageData;
import com.maibow.util.PathUtil;

/**
 * 
 * 脉博网络后台管理系统
 * 包名：com.maibow.controller.organ
 * 文件名：OrganController.java
 * 作者: 吴比(Wubi)
 * 时间：2015年4月7日-上午9:10:04
 * Copyright@ 2015湖南脉博网络科技有限公司-版权所有
 *
 */
@Controller
@RequestMapping(value = "/organ")
public class OrganController extends BaseController {

    @Resource(name = "organService")
    private OrganService organService;

    @Resource(name = "commonService")
    private CommonService commonService;

    @Resource(name = "newuserService")
    private NewUserService userService;

    @Resource(name = "siteService")
    private SiteService siteService;

    @Resource(name = "dictionaryService")
    private DictionaryService dictionaryService;

    /**
     * 
     * listUsers(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param page
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(value = "/listOrgans")
    public ModelAndView listOrgans(Page page) throws Exception {
        ModelAndView modelView = this.getModelAndView();
        PageData pageData = this.getPageData();
        String path = this.getRequest().getContextPath();
        String basePath = this.getRequest().getScheme() + "://" + this.getRequest().getServerName() + ":" + this.getRequest().getServerPort() + path + "/";

        SysOperator operator = commonService.getCurrentOperator();
        if (null == operator) {
            modelView.setViewName(this.getRequest().getContextPath() + CommonConstants.MAIN_LOGIN);
        }

        pageData.put("currentselectorganid", Integer.valueOf(operator.getOrganid()));
        List<PageData> organTree = organService.queryAllOrganListLight(pageData);
        for (PageData organ : organTree) {
            if (organ.get("is_parent") == null || organ.get("is_parent").toString().equals("1")) {
                organ.put("open", true);
            }
            organ.put("file", basePath + "/organ/listOrgansData");
        }

        JSONArray arr = JSONArray.fromObject(organTree);
        String json = arr.toString();
        json = json.replaceAll("organ_name", "name");
        modelView.setViewName("system/organ/organ_list");
        modelView.addObject("currentselectorganid", operator.getOrganid());
        modelView.addObject("zTreeNodes", json);
        modelView.addObject("pageData", pageData);
        return modelView;
    }

    /**
     * 
     * listOrgansData(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param page
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(value = "/listOrgansData")
    public ModelAndView listOrgansData(Page page) throws Exception {
        ModelAndView modelView = this.getModelAndView();
        PageData pageData = this.getPageData();
        pageData.put("currentselectorganid", Integer.valueOf(pageData.get("currentselectorganid").toString()));
        page.setPageData(pageData);
        List<PageData> organList = organService.querAllOrganlistPage(page);
        for (PageData listPagedata : organList) {
            PageData organPageData = new PageData();
            organPageData.put("organid", Integer.valueOf(listPagedata.get("parentorganid").toString()));
            organPageData = organService.getOrganInfoById(organPageData);
            listPagedata.put("parentorganname", organPageData.get("organname"));

            // if (null !=
            // listPagedata.getString("siteid")) {
            // String[] siteid =
            // listPagedata.getString("siteid").split(";");
            // Province province =
            // siteService.getProvinceInfoByCode(siteid[0]);
            // City city =
            // siteService.getCityInfoByCode(siteid[1]);
            // Area area =
            // siteService.getAreaInfoByCode(siteid[2]);
            //
            // listPagedata.put("address",
            // province.getProvincename() +
            // city.getCityname() +
            // area.getAreaname() +
            // listPagedata.getString("address"));
            // listPagedata.put("siteid",
            // province.getProvincename() +
            // city.getCityname() +
            // area.getAreaname());
            // }
            Province province = null;
            if (null != listPagedata.getString("provincecode")) {
                province = siteService.getProvinceInfoByCode(listPagedata.get("provincecode").toString());
            }

            City city = null;
            if (null != listPagedata.getString("citycode")) {
                city = siteService.getCityInfoByCode(listPagedata.get("citycode").toString());
            }

            Area area = null;
            if (null != listPagedata.getString("areacode")) {
                area = siteService.getAreaInfoByCode(listPagedata.get("areacode").toString());
            }

            if (null != province && null != city && area != null) {
                listPagedata.put("address", province.getProvincename() + city.getCityname() + area.getAreaname() + listPagedata.getString("address"));
                listPagedata.put("siteid", province.getProvincename() + city.getCityname() + area.getAreaname());
            }

            String dicvalue = listPagedata.get("organtypeid").toString();
            Dictionary dictionary = dictionaryService.queryDictionaryByCodeAndValue(BusinessConstants.BUSI_CONS_COMMON_DIC_ORGAN_TYPE, dicvalue);
            listPagedata.put("organtypename", dictionary.getDiccode());
        }

        SysOperator operator = commonService.getCurrentOperator();
        if (null == operator) {
            modelView.setViewName(this.getRequest().getContextPath() + CommonConstants.MAIN_LOGIN);
        }

        pageData.put("currentorganid", operator.getOrganid());
        modelView.setViewName("system/organ/organ_listdata");
        modelView.addObject("organList", organList);
        modelView.addObject("pageData", pageData);
        return modelView;
    }

    /**
     * 
     * goAddUser(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @Token(save = true)
    @RequestMapping(value = "/goAddOrgan")
    public ModelAndView goAddOrgan() throws Exception {
        ModelAndView modelView = this.getModelAndView();
        PageData pageData = this.getPageData();
        if (pageData.getString("currentselectorganid") == null || "".equals(pageData.getString("currentselectorganid"))) {
            pageData.put("currentselectorganid", 0);
        }

        PageData organPageData = new PageData();
        organPageData.put("organid", Integer.valueOf(pageData.get("currentselectorganid").toString()));
        organPageData = organService.getOrganInfoById(organPageData);
        pageData.put("parentorganname", organPageData.get("organname"));

        List<Dictionary> dictionaryList = dictionaryService.getDictionaryListByCode(BusinessConstants.BUSI_CONS_COMMON_DIC_ORGAN_TYPE);
        Dictionary dictionary = new Dictionary();
        dictionary.setDicvalue(dictionaryList.get(0).getDicvalue());

        modelView.setViewName("system/organ/organ_add");
        modelView.addObject("pageData", pageData);
        modelView.addObject("dictionaryList", dictionaryList);
        modelView.addObject(dictionary);
        return modelView;
    }

    /**
     * 
     * saveUser(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param out
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @Token(remove = true)
    @RequestMapping(value = "/saveOrgan")
    public ModelAndView saveOrgan(HttpServletRequest request, @RequestParam(required = false) MultipartFile organlogo, @RequestParam(required = false) MultipartFile organplane,
                        @RequestParam(required = false) MultipartFile organfrontcover, @RequestParam(value = "currentselectorganid", required = false) String currentselectorganid,
                        @RequestParam(value = "product_name", required = false) String product_name, @RequestParam(value = "dicvalue", required = false) String dicvalue,
                        @RequestParam(value = "organname", required = false) String organname, @RequestParam(value = "abbrname", required = false) String abbrname,
                        @RequestParam(value = "provincecode", required = false) String provincecode, @RequestParam(value = "citycode", required = false) String citycode,
                        @RequestParam(value = "areacode", required = false) String areacode, @RequestParam(value = "address", required = false) String address,
                        @RequestParam(value = "email", required = false) String email, @RequestParam(value = "phonenumber", required = false) String phonenumber,
                        @RequestParam(value = "introduction", required = false) String introduction, @RequestParam(value = "status", required = false) String status) throws Exception {
        ModelAndView modelView = this.getModelAndView();
        PageData pageData = this.getPageData();
        pageData.put("organname", organname);
        pageData.put("abbrname", abbrname);
        pageData.put("provincecode", provincecode);
        pageData.put("citycode", citycode);
        pageData.put("areacode", areacode);
        pageData.put("address", address);
        pageData.put("email", email);
        pageData.put("phonenumber", phonenumber);
        pageData.put("introduction", introduction);
        pageData.put("createtime", DateUtil.formatTime(DateUtil.getTime()));
        pageData.put("parentorganid", Integer.valueOf(currentselectorganid));
        pageData.put("status", Integer.valueOf(status));
        if (null == organService.findOrganInfoByOrganName(pageData)) {
            SysOperator operator = commonService.getCurrentOperator();
            if (null == operator) {
                modelView.setViewName(this.getRequest().getContextPath() + CommonConstants.MAIN_LOGIN);
            }
            pageData.put("creatorid", operator.getOperid());
            pageData.put("creatorname", operator.getNickname());

            // String siteid =
            // pageData.get("provincecode").toString()
            // + ";" +
            // pageData.get("citycode").toString()
            // + ";" +
            // pageData.get("areacode").toString();
            // pageData.put("siteid", siteid);
            pageData.put("organtypeid", Integer.valueOf(dicvalue));

            String ffile = DateUtil.getDays(), fileName = "";
            String filePath = null;
            if (null != organlogo && !organlogo.isEmpty()) {
                filePath = PathUtil.getUploadImagePath() + CommonConstants.FILEPATHIMG + ffile + "_logo/"; // 文件上传路径
                fileName = FileUpload.fileUp(organlogo, filePath, this.get32UUID()); // 执行上传
            } else {
                System.out.println("上传失败");
            }

            // ImageUtils.resize(filePath +
            // fileName, filePath + "/80!80_" +
            // fileName, 80, 80);
            // String organlogostr =
            // CommonConstants.FILEPATHIMG + ffile
            // + "_logo" + "/80!80_" + fileName;

            // String xmpath =
            // PathUtil.getUploadImagePath() +
            // CommonConstants.FILEPATHIMG + ffile
            // + "_logo/"; // 项目路径
            // fileName = xmpath.trim() +
            // fileName;
            String organlogostr = CommonConstants.FILEPATHIMG + ffile + "_logo/" + fileName;
            // 'file:/'
            File f = new File(fileName.trim());
            if (f.exists()) {
                f.delete();
            } else {
                logger.debug("图片不存在");
            }
            if (null != fileName && !"".equals(fileName)) {
                pageData.put("organlogo", organlogostr); // 路径
            }

            String ffileplane = DateUtil.getDays(), fileNamePlane = "";
            String filePathPlane = null;
            if (null != organplane && !organplane.isEmpty()) {
                filePathPlane = PathUtil.getUploadImagePath() + CommonConstants.FILEPATHIMG + ffileplane + "_plans"; // 文件上传路径
                fileNamePlane = FileUpload.fileUp(organplane, filePathPlane, this.get32UUID()); // 执行上传
            } else {
                System.out.println("上传失败");
            }
            String organplanestr = CommonConstants.FILEPATHIMG + ffileplane + "_plans/" + fileNamePlane;
            if (null != fileNamePlane && !"".equals(fileNamePlane)) {
                pageData.put("organplane", organplanestr); // 路径
            }

            String ffilefrontcover = DateUtil.getDays(), fileNameFrontcover = "";
            String filePathFrontcover = null;

            if (null != organfrontcover && !organfrontcover.isEmpty()) {
                filePathFrontcover = PathUtil.getUploadImagePath() + CommonConstants.FILEPATHIMG + ffilefrontcover + "_frontcover"; // 文件上传路径
                fileNameFrontcover = FileUpload.fileUp(organfrontcover, filePathFrontcover, this.get32UUID()); // 执行上传
            } else {
                System.out.println("上传失败");
            }

            String organfrontcoverstr = CommonConstants.FILEPATHIMG + ffilefrontcover + "_frontcover/" + fileNameFrontcover;
            if (null != fileNameFrontcover && !"".equals(fileNameFrontcover)) {
                pageData.put("organfrontcover", organfrontcoverstr); // 路径
            }

            // 创建机构
            organService.createOrganInfo(pageData, organname, organlogostr);
            modelView.addObject("msg", "success");
        } else {
            modelView.addObject("msg", "failed");
        }

        modelView.setViewName("save_result");
        return modelView;
    }

    /**
     * 
     * goEditOrgan(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @Token(save = true)
    @RequestMapping(value = "/goEditOrgan")
    public ModelAndView goEditOrgan() throws Exception {
        ModelAndView modelView = this.getModelAndView();
        PageData pageData = new PageData();
        pageData = this.getPageData();
        pageData.put("organid", Integer.valueOf(pageData.get("organid").toString()));
        PageData resultPageData = organService.getOrganInfoById(pageData); // 根据ID读取

        PageData organPageData = new PageData();
        organPageData.put("organid", Integer.valueOf(resultPageData.get("parentorganid").toString()));
        organPageData = organService.getOrganInfoById(organPageData);
        // if (resultPageData.get("siteid") !=
        // null) {
        // String[] siteid =
        // resultPageData.get("siteid").toString().split(";");
        // resultPageData.put("provincecode",
        // siteid[0]);
        // resultPageData.put("citycode",
        // siteid[1]);
        // resultPageData.put("areacode",
        // siteid[2]);
        // }

        List<Dictionary> dictionaryList = dictionaryService.getDictionaryListByCode(BusinessConstants.BUSI_CONS_COMMON_DIC_ORGAN_TYPE);
        Dictionary dictionary = new Dictionary();
        dictionary.setDicvalue(resultPageData.get("organtypeid").toString());

        resultPageData.put("parentorganname", organPageData.get("organname"));
        resultPageData.put("currentselectorganid", pageData.get("currentselectorganid"));
        modelView.setViewName("system/organ/organ_edit");
        modelView.addObject("pageData", resultPageData);
        modelView.addObject("dictionaryList", dictionaryList);
        modelView.addObject(dictionary);
        return modelView;
    }

    /**
     * 
     * editOrgan(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param out
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @Token(remove = true)
    @RequestMapping(value = "/editOrgan")
    public ModelAndView editOrgan(HttpServletRequest request, @RequestParam(required = false) MultipartFile organlogo,
                        @RequestParam(value = "organlogohidden", required = false) String organlogohidden, @RequestParam(required = false) MultipartFile organplane,
                        @RequestParam(value = "organplanehidden", required = false) String organplanehidden, @RequestParam(required = false) MultipartFile organfrontcover,
                        @RequestParam(value = "organfrontcoverhidden", required = false) String organfrontcoverhidden, @RequestParam(value = "organid", required = false) String organid,
                        @RequestParam(value = "currentselectorganid", required = false) String currentselectorganid, @RequestParam(value = "product_name", required = false) String product_name,
                        @RequestParam(value = "dicvalue", required = false) String dicvalue, @RequestParam(value = "organtypeid", required = false) String organtypeid,
                        @RequestParam(value = "organname", required = false) String organname, @RequestParam(value = "abbrname", required = false) String abbrname,
                        @RequestParam(value = "provincecode", required = false) String provincecode, @RequestParam(value = "citycode", required = false) String citycode,
                        @RequestParam(value = "areacode", required = false) String areacode, @RequestParam(value = "address", required = false) String address,
                        @RequestParam(value = "email", required = false) String email, @RequestParam(value = "phonenumber", required = false) String phonenumber,
                        @RequestParam(value = "introduction", required = false) String introduction, @RequestParam(value = "status", required = false) String status) throws Exception {
        ModelAndView modelView = this.getModelAndView();
        PageData pageData = new PageData();
        pageData = this.getPageData();
        // pageData.put("updateTime",
        // DateUtil.getTime().toString());
        pageData.put("organname", organname);
        pageData.put("abbrname", abbrname);
        pageData.put("provincecode", provincecode);
        pageData.put("citycode", citycode);
        pageData.put("areacode", areacode);
        pageData.put("address", address);
        pageData.put("email", email);
        pageData.put("phonenumber", phonenumber);
        pageData.put("introduction", introduction);
        pageData.put("createtime", DateUtil.formatTime(DateUtil.getTime()));
        pageData.put("parentorganid", Integer.valueOf(currentselectorganid));
        pageData.put("status", Integer.valueOf(status));

        String siteid = pageData.get("provincecode").toString() + ";" + pageData.get("citycode").toString() + ";" + pageData.get("areacode").toString();
        pageData.put("siteid", siteid);
        pageData.put("organtypeid", Integer.valueOf(dicvalue));
        pageData.put("organid", Integer.valueOf(organid));

        if (null == organlogohidden) {
            organlogohidden = "";
        }

        if (null == organplanehidden) {
            organplanehidden = "";
        }

        if (null == organfrontcoverhidden) {
            organfrontcoverhidden = "";
        }

        String ffile = DateUtil.getDays(), fileName = "";
        String filePath = null;
        if (null != organlogo && !organlogo.isEmpty()) {
            filePath = PathUtil.getUploadImagePath() + CommonConstants.FILEPATHIMG + ffile + "_logo/"; // 文件上传路径
            fileName = FileUpload.fileUp(organlogo, filePath, this.get32UUID()); // 执行上传

            // ImageUtils.resize(filePath +
            // fileName, filePath + "/80!80_" +
            // fileName, 80, 80);
            // String imageurlstr =
            // CommonConstants.FILEPATHIMG + ffile
            // + "_logo" + "/80!80_" + fileName;

            // String xmpath =
            // PathUtil.getUploadImagePath() +
            // CommonConstants.FILEPATHIMG + ffile
            // + "_logo/"; // 项目路径
            // fileName = xmpath.trim() +
            // fileName;
            String imageurlstr = CommonConstants.FILEPATHIMG + ffile + "_logo/" + fileName;

            // 'file:/'
            File f = new File(fileName.trim());
            if (f.exists()) {
                f.delete();
            } else {
                logger.debug("图片不存在");
            }
            pageData.put("organlogo", imageurlstr);
        } else {
            pageData.put("organlogo", organlogohidden);
        }

        if (null != organplane && !organplane.isEmpty()) {
            filePath = PathUtil.getUploadImagePath() + CommonConstants.FILEPATHIMG + ffile + "_plans/"; // 文件上传路径
            fileName = FileUpload.fileUp(organplane, filePath, this.get32UUID()); // 执行上传
            String imageurlstr = CommonConstants.FILEPATHIMG + ffile + "_plans/" + fileName;
            pageData.put("organplane", imageurlstr);
        } else {
            pageData.put("organplane", organplanehidden);
        }

        if (null != organfrontcover && !organfrontcover.isEmpty()) {
            filePath = PathUtil.getUploadImagePath() + CommonConstants.FILEPATHIMG + ffile + "_frontcover/"; // 文件上传路径
            fileName = FileUpload.fileUp(organfrontcover, filePath, this.get32UUID()); // 执行上传
            String imageurlstr = CommonConstants.FILEPATHIMG + ffile + "_frontcover/" + fileName;
            pageData.put("organfrontcover", imageurlstr);
        } else {
            pageData.put("organfrontcover", organfrontcoverhidden);
        }

        organService.editOrgan(pageData);
        modelView.addObject("msg", "success");
        modelView.setViewName("save_result");
        return modelView;
    }

    /**
     * 
     * goEditIDCode(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @Token(save = true)
    @RequestMapping(value = "/goEditIDCode")
    public ModelAndView goEditIDCode() throws Exception {
        ModelAndView modelView = this.getModelAndView();
        PageData pageData = new PageData();
        pageData = this.getPageData();
        pageData.put("organ_id", Integer.valueOf(pageData.get("organid").toString()));
        PageData resultPageData = organService.getOrganIDCodeInfoById(pageData); // 根据ID读取
        modelView.setViewName("system/organ/organ_idcode_edit");
        modelView.addObject("resultPageData", resultPageData);
        modelView.addObject("pageData", pageData);
        return modelView;
    }

    /**
     * 
     * editOrganIDCode(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param out
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @Token(remove = true)
    @RequestMapping(value = "/editOrganIDCode")
    public ModelAndView editOrganIDCode(PrintWriter out) throws Exception {
        ModelAndView modelView = this.getModelAndView();
        PageData pageData = new PageData();
        pageData = this.getPageData();
        pageData.put("organ_id", Integer.valueOf(pageData.get("organid").toString()));
        pageData.put("min_code", Integer.valueOf(pageData.get("min_code").toString()));
        pageData.put("max_code", Integer.valueOf(pageData.get("max_code").toString()));

        PageData organIDCodePageData = organService.getOrganIDCodeInfoById(pageData);
        if (null != organIDCodePageData) {
            organService.editOrganIDCode(pageData);
        } else {
            SysOperator operator = commonService.getCurrentOperator();
            pageData.put("oper_id", operator.getOperid());
            pageData.put("create_time", new Date());
            organService.saveOrganIDCode(pageData);
        }
        modelView.addObject("msg", "success");
        modelView.setViewName("save_result");
        return modelView;
    }

    /**
     * 
     * hasOrganName(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param out
     * 返回值：void
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(value = "/hasOrganName")
    public void hasOrganName(PrintWriter out) {
        PageData pageData = new PageData();
        try {
            pageData = this.getPageData();
            if (organService.findOrganInfoByOrganName(pageData) != null) {
                out.write("error");
            } else {
                out.write("success");
            }

            out.close();
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
    }

    /**
     * 
     * hasOrganById(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param out
     * 返回值：void
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(value = "/hasOrganById")
    public void hasOrganById(PrintWriter out) {
        PageData pageData = new PageData();
        try {
            pageData = this.getPageData();
            pageData.put("organid", Integer.valueOf(pageData.get("organid").toString()));
            if (organService.getOrganInfoById(pageData) != null) {
                out.write("success");
            } else {
                out.write("error");
            }

            out.close();
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
    }

    @RequestMapping(value = "/verifyHasChildOrgan")
    public void verifyHasChildOrgan(PrintWriter out) {
        PageData pageData = new PageData();
        try {
            pageData = this.getPageData();
            if (organService.getChildrenOrganInfoByParentId(pageData) != null) {
                out.write("false");
            } else {
                out.write("success");
            }

            out.close();
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
    }

    /**
     * 
     * deleteOrgan(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param out
     * 返回值：void
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(value = "/deleteOrgan")
    public void deleteOrgan(PrintWriter out) {
        PageData pageData = new PageData();
        try {
            pageData = this.getPageData();
            PageData verifyPageData = new PageData();
            verifyPageData.put("parentorganid", Integer.valueOf(pageData.get("organid").toString()));
            Long subOrganCount = (Long) organService.getChildrenOrganInfoByParentId(verifyPageData).get("organcount");
            if (subOrganCount > 0) {
                out.write("error");
            } else {
                pageData.put("organid", Integer.valueOf(pageData.get("organid").toString()));
                Long hasOperatorCount = (Long) userService.getOperatorInfoByOrganId(pageData).get("operatorcount");
                if (hasOperatorCount > 0) {
                    out.write("hasoperator");
                } else {
                    organService.deleteOrganById(pageData);
                    out.write("success");
                }
            }
            out.close();
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }

    }

    // 删除图片
    @ResponseBody
    @RequestMapping(value = "/deltp/{url1}/{url2}/{url3}/{tpurl}/{target}/{organid}", method = RequestMethod.POST)
    public ModelAndView deltp(@PathVariable String url1, @PathVariable String url2, @PathVariable String url3, @PathVariable String tpurl, @PathVariable String target, @PathVariable String organid,
                        HttpServletRequest request, HttpServletResponse response) {
        logBefore(logger, "删除图片");
        ModelAndView modelView = new ModelAndView();
        String msg = null;
        try {
            PageData pageData = new PageData();
            pageData = this.getPageData();
            pageData.put("organid", Integer.valueOf(organid));
            pageData.put("target", target);

            // 图片路径
            if (tpurl != null) {
                // 删除硬盘上的文件 start
                String xmpath = PathUtil.getUploadImagePath(); // 项目路径
                tpurl = xmpath.trim() + url1 + "/" + url2 + "/" + url3 + "/" + tpurl;

                // 'file:/'
                File f = new File(tpurl.trim());
                if (f.exists()) {
                    f.delete();
                } else {
                    logger.debug("图片不存在");
                }
                // 删除硬盘上的文件 end
                organService.delTp(pageData); // 删除数据中图片数据
            }

            msg = "success";
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }

        modelView.addObject(msg);
        return modelView;
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(format, true));
    }
}
